package com.depot.sys.dao;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import com.base.KeyGenerator;
import com.depot.DepotConstants;
import com.depot.domains.Users;
import com.soar.context.Environment;
import com.soar.encrypt.SoarMd5;
import com.soar.jdbc.dbhelper.DbParameter;
import com.soar.jdbc.dbhelper.DbTemplates;
import com.soar.jdbc.dbhelper.SameTypes;
import com.soar.jdbc.dbhelper.SqlTypes;
import com.soar.jdbc.dbhelper.WhereTypes;
import com.soar.util.net.IPUtils;

/**
 * 
 * @author zhangaoxiang
 * @version $Revision$ - $Author$ at $Date$
 * @description com.itaskstar.sys.web.UserLoginDao
 */
public class UserLoginDao
{

	/**
	 * 判断是不是组织被禁 1 正常 0 被禁 2 不存在
	 * 
	 * @param env
	 * @param domain
	 * @return
	 * @throws SQLException
	 */
	public static String userOrgStatus(Environment env, Users domain) throws SQLException
	{
		DbParameter param = null;
		param = new DbParameter("ORGANIZATION T1");
		param.setSelectFields("T1.STATUS");
		param.setWhereParams("T1.ID", "(SELECT T2.ORG_ID FROM USERS T2 WHERE T2.STATUS ='1'  AND  T2.MOBILE='" + domain.getMobile() + "')", SqlTypes.CUSTOM, WhereTypes.AND, SameTypes.YES);
		List<HashMap<String, String>> list = DbTemplates.select(env.getConnection(), param);
		if (list.size() == 1)
		{
			String re = list.get(0).get("STATUS");
			if ("1".equals(re))
			{
				return "1";
			}
			else
			{
				return "0";
			}
		}
		else
		{
			return "2";
		}

	}

	public static HashMap<String, String> dologin(Environment env, Users domain) throws SQLException
	{
		DbParameter param = null;
		param = new DbParameter("users T1,ORGANIZATION T2");
		param.setSelectFields("T1.ID,T1.NAME,T1.ORG_ID,T2.ORG_NAME,T2.ORG_CODE");
		param.setWhereParams("T1.MOBILE", domain.getMobile(), SqlTypes.VARCHAR, WhereTypes.AND, SameTypes.YES);
		param.setWhereParams("T1.PASSWORD", new SoarMd5().getMd5(domain.getPassword()), SqlTypes.VARCHAR, WhereTypes.AND, SameTypes.YES);
		param.setWhereParams("T1.STATUS", "1", SqlTypes.VARCHAR, WhereTypes.AND, SameTypes.YES);// 用户未禁用STATE
		param.setWhereParams("T1.ORG_ID", "T2.ID(+)", SqlTypes.CUSTOM, WhereTypes.NOT, SameTypes.YES);
		List<HashMap<String, String>> list = DbTemplates.select(env.getConnection(), param);
		if (list.size() == 1)
		{
			return list.get(0);
		}
		else
		{
			return null;
		}
	}

	public static String getRolecodeByUserid(Environment env, String id) throws SQLException
	{
		DbParameter param = new DbParameter("USER_ROLE T2,APP_ROLE T3");

		param.setSelectFields("T3.ROLE_CODE");
		param.setWhereParams("T2.USER_ID", id, SqlTypes.CHAR, WhereTypes.AND, SameTypes.YES);
		param.setWhereParams("T3. ID", "T2.ROLE_ID", SqlTypes.CUSTOM, WhereTypes.AND, SameTypes.YES);

		List<HashMap<String, String>> list = DbTemplates.select(env.getConnection(), param);
		String rolecodeStr = "";

		if (list.size() > 0)
		{
			for (HashMap<String, String> map : list)
			{
				rolecodeStr += "," + map.get("ROLE_CODE");
			}
			if (rolecodeStr.contains(DepotConstants.ORG_ORDINARY_CODE))
			{
				return DepotConstants.ORG_ORDINARY_CODE;
			}
			else if (rolecodeStr.contains(DepotConstants.ORG_SERVICE_CODE))
			{
				return DepotConstants.ORG_SERVICE_CODE;
			}
			else if (rolecodeStr.contains(DepotConstants.ORG_LOGISTIC_CODE))
			{
				return DepotConstants.ORG_LOGISTIC_CODE;
			}
			else if (rolecodeStr.contains(DepotConstants.ORG_FINANCIAL_CODE))
			{
				return DepotConstants.ORG_FINANCIAL_CODE;
			}
			else if (rolecodeStr.contains(DepotConstants.DEPOT_ADMIN_CODE))
			{
				return DepotConstants.DEPOT_ADMIN_CODE;
			}
			else if (rolecodeStr.contains(DepotConstants.ADMIN_CODE))
			{
				return DepotConstants.ADMIN_CODE;
			}
			
			else
			{
				return null;
			}
		}
		else
		{
			return null;
		}
	}

	public static void loginRecord(Environment env,String logindec,String mobile,String loginStatus) throws SQLException
	{
		String ip=IPUtils.getRemoteIpAddr(env.getRequest());
		DbParameter param = null;
		param = new DbParameter("LOGIN_RECORD T1");
		String rid = KeyGenerator.getInstance().getKeyId(env.getConnection());
		param.setParams("T1.ID", rid, SqlTypes.CHAR);
		param.setParams("T1.LOGIN_NAME", mobile, SqlTypes.VARCHAR);
		param.setParams("T1.IP", ip, SqlTypes.VARCHAR);
		param.setParams("T1.STATUS", loginStatus, SqlTypes.CHAR);
		param.setParams("T1.LOGIN_LOG", logindec, SqlTypes.CHAR);
		
		DbTemplates.insert(env.getConnection(), param);
	}
}
